package com.ftg.learn.controller;

import com.ftg.learn.service.AiServiceKjx;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 布衣
 */
@RestController
public class ChatDemoController {

//    //异步
//    private final StreamingChatModel chat;
//    //同步
//    private final ChatModel chatModel;
//
//    public ChatDemoController(StreamingChatModel chat, ChatModel chatLanguageModel) {
//        this.chat = chat;
//        this.chatModel = chatLanguageModel;
//    }

    @Resource
    private AiServiceKjx aiService;

    @GetMapping("/say2")
    public String say2(@RequestParam("message") String message) {
        return aiService.chat(message);
    }

//    @GetMapping("/say1")
//    public String say1(@RequestParam("message") String message) {
//        // 使用非流式方法获取完整响应
////        chatModel.ch
//        UserMessage u = new UserMessage("张三",message);
//        String response = chatModel.chat(u).aiMessage().text();
//        return response;
//    }
//
//    @GetMapping(value = "/say", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public SseEmitter say(@RequestParam("message") String message) throws IOException {
//        SseEmitterUTF8 emitter = new SseEmitterUTF8(Long.MAX_VALUE);
//
//        StringBuilder messageBuffer = new StringBuilder();
//        // 使用StreamingChatModel进行流式聊天
//        chat.chat(Collections.singletonList(new UserMessage(message)),
//                new StreamingChatResponseHandler() {
//                    @Override
//                    public void onPartialResponse(String partialResponse) {
//                        try {
//                            messageBuffer.append(partialResponse);
//                            // 发送每个响应片段到客户端
//                            emitter.send(messageBuffer.toString());
//                        } catch (Exception e) {
//                            emitter.completeWithError(e);
//                        }
//                    }
//
//                    @Override
//                    public void onCompleteResponse(ChatResponse completeResponse) {
//                        emitter.complete();
//                    }
//
//                    @Override
//                    public void onError(Throwable error) {
//                        emitter.completeWithError(error);
//                    }
//                });
//
//        return emitter;
//    }
}
